﻿using System;
using OpenTK;
using OpenTK.Graphics.OpenGL;
using System.Drawing;

namespace Pong
{
    class PongGame : GameWindow
    {
        InputController inputController;
        PaddleController paddleController;
        BallController ballController;
        Paddle paddle;
        Ball ball;

       
        public float windowSize = 30;
        
        public PongGame(int widht, int height) : base(widht, height)
        {
            inputController = new InputController();
            paddle = new Paddle();
            paddleController = new PaddleController(paddle);
            ball = new Ball();
            ballController = new BallController(ball);
        }

       

        private void drawFrame(float BorderSize,Color BorderColor)
        {
            GL.PushMatrix();
            GL.Color3(BorderColor);
            GL.Rect(-windowSize, windowSize, windowSize, windowSize-BorderSize); // TopBorder
            GL.Rect(-windowSize,-windowSize,-windowSize+BorderSize, windowSize); // LeftBorder
            GL.Rect(-windowSize,-windowSize, windowSize,-windowSize+BorderSize); // BottomBorder
            GL.Rect( windowSize-BorderSize, windowSize, windowSize,-windowSize); // RightBorder
            GL.PopMatrix();
        }
          
      
        protected override void OnLoad(EventArgs e)
        {
 	        base.OnLoad(e);
            GL.Enable(EnableCap.DepthTest);
            GL.Enable(EnableCap.ColorMaterial);
            GL.ClearColor(Color.Black);
        }

        protected override void OnResize(EventArgs e)
        {
 	        base.OnResize(e);
            GL.Viewport(0, 0, Width, Height);
            float aspect = Width / Height;
            Matrix4 perspective = Matrix4.CreatePerspectiveFieldOfView(MathHelper.PiOver2, aspect, 1.0f, 100.0f);
            GL.MatrixMode(MatrixMode.Projection);
            GL.LoadMatrix(ref perspective);
        }

        protected override void OnClosed(EventArgs e)
        {
 	        base.OnClosed(e);
            this.Dispose();
        }

        protected override void OnUpdateFrame(FrameEventArgs e)
        {
 	        base.OnUpdateFrame(e);
            inputController.Update(this);
            ballController.Update(this);
            paddleController.Update(this);

        }

        public bool colisionDetecter(Vector4 Obj1, Vector4 Obj2)
        {
            if (    Obj1.Z < Obj2.Y) // Bottom 
            {
                return true;
            }
            else if (Obj1.Y > Obj2.Z) // Top
            {
                return true;
            }
            else if (Obj1.W < Obj2.X) // Right
            {
                return true;
            }
            else if (Obj1.X > Obj2.W) // Left
            {
                return true;
            }
            else
                return false;
        }


        protected override void OnRenderFrame(FrameEventArgs e)
        {
 	        base.OnRenderFrame(e);
            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
            GL.MatrixMode(MatrixMode.Modelview);
            Matrix4 modelview = Matrix4.LookAt(0,0, windowSize , 0, 0, 0, 0, .1f, 0);
            GL.LoadMatrix(ref modelview);
            drawFrame(1.5f,Color.White);
            
            GL.Color3(Color.Red);
                GL.PushMatrix();
                    GL.Translate(-windowSize + 4, 0, 0);
                    paddle.Render();
                GL.PopMatrix();
            ball.Render();
            SwapBuffers();
        }
    }
}
